#include "metrics/Builder.hpp"
#include "metrics/Counter.hpp"

namespace metrics
{
    Counter::Counter(double v) : _value(v)
    {
    }

    Counter::~Counter()
    {
    }

    void Counter::increment(double value)
    {
        change(value);
    }

    double Counter::value() const
    {
        return _value.load();
    }

    ClientMetric Counter::collect() const
    {
        ClientMetric metric;
        metric.counter.value = value();
        return metric;
    }

    void Counter::change(double value)
    {
        double oldv = 0;
        double newv = 0;
        do
        {
            oldv = _value.load();
            newv = oldv + value;
        } while (!_value.compare_exchange_weak(oldv, newv));
    }

    Builder<Counter> BuildCounter() { return {}; }
} // namespace metrics
